

import de.medieninf.sensor.SensData;
import de.medieninf.sensor.SensorReceiver;
import de.medieninf.sensor.SensorReceiverListener;

public class SampleReceive {
	
	private static class SampleListener implements SensorReceiverListener {
		public void handle(SensData sensData) {
			Object[] args = {
					Double.valueOf(sensData.getX()),
					Double.valueOf(sensData.getY()),
					Double.valueOf(sensData.getZ()),
			        sensData.getKeys(),
			        Double.valueOf(sensData.getTimestamp()),
			        sensData.getDeviceId()};
			System.out.format("{x:%9.4f, y:%9.4f, z:%9.4f, k:%8s, timestamp:%10.2f, devid: %s}\n", args);
		}
	}
	
	public static void main(String[] argv) {
		int receiveTime = 60000;
		if (argv.length > 0)
			receiveTime = Integer.parseInt(argv[0]);
		System.out.println("Receive for " + receiveTime + " milliseconds (change with arg) on default port");
		SensorReceiver sensorReceiver = new SensorReceiver();
		sensorReceiver.setListener(new SampleListener());
		sensorReceiver.startReceiving();
		System.out.println("Receiving for " + receiveTime/1000 + " seconds.");
		try {
			Thread.sleep(60000);
		} catch (InterruptedException e) {
			System.out.println("Interrupted, ignored");
		}
		System.out.println("Stop Receiving");
		sensorReceiver.stopReceiving();
	}
}
